<?php

namespace app\admin\controller\metting;

use app\common\controller\Backend;

use think\Controller;
use think\Request;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Log extends Backend
{
    
    /**
     * MettingLog模型对象
     */
    protected $model = null;
    protected $searchfields = 'code,name';
    protected $communityModel = null;
    protected $buildingModel = null;
    protected $roomModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('MettingLog');
        $this->communityModel = model('Community');
        $this->buildingModel = model('Building');
        $this->roomModel = model('MettingRoom');
        $this->view->assign('rooms', $this->getRooms());
        $this->view->assign('statusList', ['待审核', '审核通过', '已拒绝']);
//        $this->view->assign('community',$this->communityModel->where(array('code'=>array('in',parent::getCommunityIdByAuth())))->field('code,name')->select());

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function getRooms()
    {
//        echo json_encode($this->roomModel->select());
//        exit();
//        var_dump($this->roomModel->with('Building,Community')->select());
//        exit();
        return $this->roomModel->with('Building,Community')->select();
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'htmlspecialchars']);
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with('mettingroom')
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with('mettingroom')
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    private function handleSearch($searchfields=null) {
        $append = array(
            array('community_id','in',parent::getCommunityIdByAuth())
        );
        $append = array_merge($append,$this->buildCommonSearch());
        list($where, $sort, $order, $offset, $limit,$orderParams) = $this->buildparams($searchfields,null,$append);
        $total = $this->model->where($where)->count();
        $list = $this->model->with('community,building')->where($where)->order($orderParams)->limit($offset, $limit)->select();
        $result = array("total" => $total, "rows" => $list);
        return json($result);
    }

    /**
     * 自定义搜索
     * @return array
     */
    private function buildCommonSearch() {
        $where = array();
        $searchs = $this->request->request('query/a');
        if ($searchs['community_id']) {
            $where[] = array('community_id', '=', $searchs['community_id']);
        }
        if ($searchs['building_code']) {
            $where[] = array('building_code', '=', $searchs['building_code']);
        }
        if ($searchs['enter_time_begin']) {
            $where[] = array('enter_time', '>=', strtotime($searchs['enter_time_begin'].'00:00:00'));
        }
        if ($searchs['enter_time_end']) {
            $where[] = array('enter_time', '<=', strtotime($searchs['enter_time_end'].'23:59:59'));
        }
        if ($searchs['owner_name']) {
            $where[] = array('owner_name', 'like', sprintf('%%%s%%',$searchs['owner_name']));
        }
        if ($searchs['owner_tel']) {
            $where[] = array('owner_tel', 'like', sprintf('%%%s%%',$searchs['owner_tel']));
        }
        return $where;
    }
}
